package guanying;

import java.util.*;

public class Cinema {
    static Scanner sc = new Scanner(System.in);
    static ArrayList<Movies> movies = new ArrayList<>();
    static Map<String,String> users = new HashMap<>();

    static {
        movies.add(new Movies("唐顿庄园", "动作", 100));
        movies.add(new Movies("新警察故事", "动作", 999));
        movies.add(new Movies("澳门风云3", "动作", 100));
        movies.add(new Movies("憨豆先生", "搞笑", 666));
    }

    public static void main(String[] args) {
        while (true) {
            System.out.println("欢迎来到观影指南");
            System.out.println("1.注册账号：");
            System.out.println("2.登录账号：");
            System.out.println("3.退出系统：");
            switch (sc.nextInt()) {
                case 1 -> register();
                case 2 -> login();
                case 3 -> {
                    System.out.println("退出系统");
                    return;
                }
                default -> System.out.println("输入错误");
            }
        }
    }

    //登录
    public static void login() {
        //定义登录次数
        int count = 0;
        while (true) {
            System.out.println("请输入用户名：");
            String userName = sc.next();
            System.out.println("请输入密码：");
            String password = sc.next();
            if (users.containsKey(userName)&& users.get(userName).equals(password)) {//账号和密码一致
                System.out.println("登录成功！");
                menu();//菜单
                return;
            }
            else {//账号和密码不一致
                System.out.println("用户名或密码错误");
                count++;
            }
            if (count >= 3) {
                System.out.println("输入错误次数过多，请重新输入");
                return;
            }
        }
    }

    //注册
    public static void register() {
        System.out.println("请输入用户名：");
        String userName = sc.next();
        System.out.println("请输入密码：");
        String password = sc.next();
        users.put(userName, password);
        System.out.println("注册成功！");
    }

    public static void menu() {
        while (true) {
            System.out.println("欢迎使用观影指南管理器");
            System.out.println("1.添加电影");
            System.out.println("2.查看电影");
            System.out.println("3.显示人气最高电影");
            int choice = sc.nextInt();
            switch (choice) {
                case 1 -> addMovie();
                case 2 -> {
                    System.out.println("查看电影");
                    viewMovie();
                }
                case 3 -> {
                    System.out.println("显示人气最高电影");
                    viewHotMovie();
                }
                default -> System.out.println("输入错误");
            }
        }
    }

    //添加电影
    public static void addMovie() {
        System.out.println("请输入电影名称：");
        String name = sc.next();
        System.out.println("请输入电影类型：");
        String type = sc.next();
        System.out.println("请输入电影人气：");
        int popularity = sc.nextInt();
        movies.add(new Movies(name, type, popularity));
        System.out.println("添加成功！");
    }

    //查看电影
    public static void viewMovie() {
        for (Movies movie : movies) {
            System.out.println("电影名称：" + movie.getName());
            System.out.println("电影类型：" + movie.getType());
            System.out.println("电影人气：" + movie.getPopularity()+"\n");
        }
    }

    //显示人气最高电影
    public static void viewHotMovie() {
        if (movies.isEmpty()){
            System.out.println("当前没有电影");
            return;
        }
        //定义一个最高分数的电影
        Movies maxScore = movies.get(0);
        for (Movies movie : movies) {//遍历电影
            if (movie.getPopularity() > maxScore.getPopularity()) {//比较分数
                maxScore = movie;//赋值给最高分数
            }
        }
        //输出最高分数
        System.out.println("电影名称：" + maxScore.getName());
        System.out.println("电影类型：" + maxScore.getType());
        System.out.println("电影人气：" + maxScore.getPopularity());
    }
}
